<?php
namespace app\manager\controller;

use think\Controller;
use think\Request;
use app\common\traits\controller\TraitUpload;
/**
 * 会员管理
 */
class User extends BaseController
{
	use TraitUpload;
	const WAIT_CHECK = 0;//待审核
	const CHECKED = 1;//审核通过
	const UNCHECKED = -99;//审核未通过

	//会员信息列表
	public function getUserList()
	{
		$list = model('Users')->getUserList(input('get.'));//halt($list);
		$this->assign('list',$list);
		return view('p/user/index');
	}
	

	public function getCheck($id)
	{
		$userinfo = model('Users')->getUserInfo($id);
		$camp_id = $userinfo->camp_id;
		$this->assign('camp_id',$camp_id);

		$caravaninfo = model('Caravans')->where('user_id',$id)->find();
		$caravan_id = $caravaninfo['id'];
		$this->assign('caravaninfo',$caravaninfo);//halt($caravaninfo);
		$this->assign('userinfo',$userinfo);
		return view('p/user/check');
	}

	public function getRead($id)
	{
		$userinfo = model('Users')->getUserInfo($id);//halt($userinfo);
		$this->assign('userinfo',$userinfo);

		$caravan_name = model('Caravans')->where('user_id',$id)->value('name');
		$this->assign('caravan_name',$caravan_name);

		$contactioninfo = db('contactioninfo')->where('user_id',$id)->select();
		$this->assign('contactioninfo',$contactioninfo);//新增联系人

		return view('p/user/read');
	}

	public function postChecked()
	{
		$id = input('post.id');//用户ID
		$status = input('post.status');//审核状态
		$refuse = '审核不通过：'.input('post.refuse');//拒绝理由
		$caravan_id = input('post.caravan_id');//房车ID
		if ($status == self::CHECKED) {
			$count = model('Users')->changeStatus($id ,$status);
			if ($count) {
				model('Calendar')->initCaravanCarlendar($caravan_id, $y = 1);//初始化房车日历
				model('Messages')->createMsg($msg_info = ['users_id' => $id, 'body' => '恭喜您!审核通过!请尽快完善房车信息,提高交换率!']);
				return $this->success('审核通过', '/user/userlist');
			}
		}
		
		if ($status == self::UNCHECKED) {
			$count = model('Users')->changeStatus($id,$status);
			if ($count) {
				model('Messages')->createMsg($msg_info = ['users_id' => $id, 'body' => $refuse]);
				return $this->error('拒绝通过审核', '/user/userlist');
			}
		}
		
		
	}

	public function postUpload()
	{
    	if ($this->upload('upfile')) {
        $url = $this->getUploadInfos();
        return json(array(
                "state" => 'SUCCESS',
                "url" => $url['url'],
            ));
      }
    }

	//////////////////////////////////实名认证模块//////////////////////////////////////
    /**
     * [FunctionName 列表]
     * @param string $value [description]
     */
    public function getRZList()
    {
    	$id = session('manager.id');
        $camp_id = model('CampManagers')->where('id',$id)->value('camp_id');
    	$Rzlist = db('user_certification')->where('camp_id',$camp_id)->select();
		return view('p/user/rzindex',['Rzlist'=>$Rzlist]);
    }
    /**
     * [FunctionName 查看]
     * @param string $value [description]
     */
    public function getRZInfo($id)
    {
    	$info = db('user_certification')->where('id',$id)->find();
    	$rz = db('users')->where('id',$info['user_id'])->value('authentication');
		return view('p/user/waitrenzh',['info'=>$info,'rz'=>$rz]);
    }
    /**
     * [FunctionName 认证]
     * @param string $value [description]
     */
    public function postRZPass()
    {
    	$name = input('post.name');
    	$ID_number = input('post.number');
    	$user_id = db('user_certification')->where('card_id',$ID_number)->value('user_id');
    	$res = model('Authentication')->Check($user_id,$name,$ID_number);
    	if ($res['isok']==1&&$res['code']==1) {
    		$re = db('users')->where('id',$user_id)->setField('authentication',1);
    		if ($re) {
    			return $this->success($res['messsage'],'/user/userlist');
    		}
    	}else{
    		return $this->error($res['messsage']);
    	}
    }

}